package com.example.my_flea_street.controller;

import com.example.my_flea_street.bean.Login;
import com.example.my_flea_street.bean.ResponseModel;
import com.example.my_flea_street.bean.User;
import com.example.my_flea_street.service.impl.LoginServiceImpl;
import com.example.my_flea_street.service.impl.UserServiceImpl;
import com.example.my_flea_street.util.ImageUtil;
import com.example.my_flea_street.util.ResponseHelper;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import sun.rmi.runtime.Log;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping(value = "/login")
public class LoginController {

    private final LoginServiceImpl service;

    private final UserServiceImpl userService;

    @Autowired
    public LoginController(LoginServiceImpl service, UserServiceImpl userService) {
        this.service = service;
        this.userService = userService;
    }


    @RequestMapping(value = "/login", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseEntity<ResponseModel> Login(@RequestBody Map<String, String> json) {
        String phone = json.get("phone");
        String password = json.get("password");
        System.out.println(json.get("phone") + json.get("password"));
        if (phone == null)
            return ResponseHelper.createResponse(-1, "手机号不能为空");
        if (password == null)
            return ResponseHelper.createResponse(-1, "密码不能为空");
        Login loginData = service.userIsExisted(phone, password);
        if (loginData == null)
            return ResponseHelper.createResponse(-2, "账号不存在，需要注册");
        if (!password.equals(loginData.getPassward()))
            return ResponseHelper.createResponse(-3, "密码错误");
        User user = userService.getUserByPhone(loginData.getPhone());
        user.setPassword(userService.getPassword(user.getPhone()));
        return ResponseHelper.createResponse(0, "成功", user);
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ResponseEntity<ResponseModel> Register(@RequestBody Map<String, String> json, HttpServletRequest request) {
        String str_user = json.get("user");
        String pwd = json.get("password");
        JSONObject jsonObject = JSONObject.fromObject(str_user);
        User user= (User)JSONObject.toBean(jsonObject,User.class);
        Login login = service.userIsExisted(user.getPhone(),pwd);
        if (login != null)
            return ResponseHelper.createResponse(-1, "该手机已注册!");
        Login login1 = service.userNameIsExited(user.getName());
        if (login1 != null) {
            return ResponseHelper.createResponse(-1, "用户名已存在!");
        }
        service.registerToLogin(user.getPhone(),pwd);
        String headUrl = ImageUtil.GenerateImage(user.getHead()+String.valueOf(System.currentTimeMillis()),user.getPhone(),"head");
        System.out.println(headUrl);
        if (headUrl == null) {
            System.out.println("保存图片失败！");
            service.unRegistToLogin(user.getPhone());
            return ResponseHelper.createResponse(-1,"服务器异常，保存用户头像失败！");
        }
        user.setHead(headUrl);
        userService.registerToUser(user);
        return ResponseHelper.createResponse(0, "注册成功，赶紧登陆吧");
    }

    @RequestMapping(value = "/editUser", method = RequestMethod.POST)
    public ResponseEntity<ResponseModel> editUser(@RequestBody  Map<String, String> json) {
        JSONObject jsonObject = JSONObject.fromObject(json.get("user"));
        User user= (User)JSONObject.toBean(jsonObject,User.class);
        String head64 = user.getHead();
        String head = userService.getHead(user.getId());
        if (head.equals(head64)) {
            head64 = head;
        }else {
            ImageUtil.deletePic(head,"head");
            head64 = ImageUtil.GenerateImage(head64,user.getPhone()+String.valueOf(System.currentTimeMillis()),"head");
        }
        int status = userService.editUser(user.getId(),user.getName(),head64,user.getAge()
                ,user.getInstitute(),user.getIntroduction(),user.getSchool());
        if (status == 1) {
            return ResponseHelper.createResponse(0,"成功",userService.getUserByPhone(user.getPhone()));
        }
        else return ResponseHelper.createResponse(-1,"失败");

    }

    @RequestMapping(value = "/phoneIsExited", method = RequestMethod.POST)
    public ResponseEntity<ResponseModel> phoneIsExited(@RequestBody Login phone) {
        System.out.println("lc79"+phone.getPhone() + phone.getPassward());
        Login login = service.userIsExisted(phone.getPhone(),"");
        if (login == null)
            return ResponseHelper.createResponse(-2, "账号不存在，需要注册!");
        User user = userService.getUserByPhone(login.getPhone());
        user.setPassword(userService.getPassword(user.getPhone()));
        return ResponseHelper.createResponse(0, "账号存在",user);
    }

    @RequestMapping(value = "/editPassword", method = RequestMethod.POST)
    public ResponseEntity<ResponseModel> editPassword(@RequestBody Map<String, String> json) {
        String phone = json.get("phone");
        String password = json.get("password");
        Login login = service.userIsExisted(phone,password);
        if (login == null)
            return ResponseHelper.createResponse(-1, "账号不存在，需要注册!");
        service.editPassword(phone,password);
      return ResponseHelper.createResponse(0, "修改成功");
    }

}
